var request = function()
{
    
    this.init = function(c)
    {
        var serial = c.get("serial");
        this.serial = new serial();
        this.config = c.get("config");
        this.sign = c.get("sign");
        this.msg = c.get("msg");
        this.data = {};
        this.loadingText = "";
        return this;
    };
    
    this.setData = function(data)
    {
        this.data = data;
        return this;
    };
    
    this.setSuccess = function(f)
    {
        this.serial.add(f);
        return this;
    };
    
    this.setLoadingText = function(text)
    {
        this.loadingText = text;
        return this;
    };
    
    this.run = function(name)
    {
        var that = this;
        var config = this.config;
        var appKey = config.appKey;
        var domain = config.domain;
        var list = config.list;
        var item = list[name];
        if(item.domain == ""){
            item.domain = domain;
        }
        var url = item.domain + item.url;
        var header = {};
        header = {"content-type": "application/json"};
        var data = this.data;
        if(item.sign == "1"){
            data = this.sign.sign(appKey, data);
        }
        
        function success(response)
        {
            that.serial.run(response);
        }
        
        function fail(response)
        {
            if(response.errMsg == "request:fail timeout"){
                that.msg.sure("信息", "网络超时,请重试");
            }
        }
        
        function complete()
        {
            that.setData({});
            that.serial.clear();
            if(that.loadingText != ""){
                that.setLoadingText("");
                setTimeout(that.msg.hideLoading, 500);
            }
        }
        
        if(this.loadingText != ""){
            this.msg.showLoading(this.loadingText);
        }
        uni.request({url: url, data: data, method: item.method, header: header, success: success, fail: fail, complete: complete});
    };
    
};

module.exports = request;